/**
 * Created by charsen on 16/5/25.
 */
directives.directive('upFileMsg', ["$rootScope", "tools", "$timeout", function ($rootScope, tools, $timeout) {
    return {
        restrict: 'AE',
        scope: {},
        replace: true,
        link: function (scope, element, attr) {
            //upload file
            $rootScope.$on('Up_File_EV', function () {

                scope.tableDesc = '';
                $('#upProgress').progress({
                    percent: 0
                });
                $(element).modal('show');
                //console.log(scope);
               // console.log($("#fileUpMySheet #mySheetTable tbody").find("tr").length);
                if($("#fileUpMySheet #mySheetTable tbody").find("tr").length>0){
                    scope.lastFileup = true;
                }else{
                    scope.lastFileup = false;
                }
            });

            scope.fileWatch = function(){
                $(element).modal('hide');

                $rootScope.$broadcast("showMySheet_EV","look");
                $timeout(function(){$rootScope.$broadcast("loading_Start_EV");},200);
            };

           // scope.file=null;
            scope.baseUp = function () {
                $("#fileup").click();
                $("#fileup").change(function () {
                    scope.fileName = $("#fileup").val();

                    var file = document.getElementById('fileup').files[0];
                   //
                   // scope.file =file;
                  //  console.log(scope.fileName);

                    var index = file.name.lastIndexOf(".");


                    $timeout(function(){
                        scope.tableDesc = file.name.substr(0,index);
                    },0);

                    if (file.name.substring(index) != ".xls" && file.name.substring(index) != ".xlsx") {
                        scope.msg = "请选择.xls,.xlsx文件";
                        $timeout(function () {
                            scope.msg = "";
                        }, 3000);
                        return;
                    }
                    //scope.tableDesc = fileName.substr(0,index);
                   /* var reader = new FileReader();
                    reader.readAsDataURL(file);
                    scope.data = null;
                    reader.onload = function (e) {
                        scope.data = e.target.result;
                        //$('#fileup2').val(e.target.result);
                        //console.log(scope.data);
                    };*/
                    $('.commit').removeClass('disabled');
                });
            };
            scope.isUpLoad=false;
            scope.upfileLod = function (e) {

                $('.commit').addClass('disabled');
                $(element).find("#progress").show();

                //init
                if($("#fileUpMySheet #mySheetTable tbody").find("tr").length>0){
                    $("#mySheetTable tbody").html("");
                    var uiB=$("#fileUpMySheet .ui.tab").find(".ui.button.green");
                    for(var i=0;i<uiB.length;i++){
                        uiB.eq(i).removeAttr("disabled").html("上传");
                    }
                }

               /* $('#upProgress2').progress({
                    percent: 0
                });
                $('#upProgress2').show();*/

                //return;
                var timer;
                function goTime(x){
                    if(x>80){
                        clearTimeout(timer);
                    }else{
                       /* $('#upProgress2').progress({
                            percent: x
                        });*/

                        $('#upProgress').progress({
                            percent: x
                        });
                        x=x+2;
                        timer=setTimeout(function(){goTime(x);},350);
                    }
                }
                function goTimeEnd(x){
                    if(x>100){
                        clearTimeout(timer);
                        console.log("结束");

                        $('#upProgress').progress({
                            percent: 100
                        });
                       /* $('#upProgress2').progress({
                            percent: 100
                        });*/

                        $timeout(function(){
                            $(element).modal('hide');
                            $(element).find("#progress").hide();
//                            $('#upProgress').hide();
                            scope.tableDesc = null;
                        },300);
                        $timeout(function(){
                            $rootScope.$broadcast("showMySheet_EV");

                        },300);
                       // $timeout(function(){$rootScope.$broadcast("loading_Start_EV");},150);

                        $("#fileup").val('');

                        scope.filebtn = false;
                        scope.fileName = null;

                        scope.data = null;
                        $('.commit').addClass('disabled').html('提交');
                        return;
                    }else{
                        $('#upProgress').progress({
                            percent: x
                        });
                       /* $('#upProgress2').progress({
                            percent: x
                        });*/
                        x=x+5;
                       // console.log(x);
                        timer=setTimeout(function(){goTimeEnd(x);},100);
                    }
                }

                var formData = new FormData($('#formData')[0]);

                $.ajax({
                    type:'POST',
                    /*url:'/Ibm-Client/uploadFile.htm',
                    dataType:'json',
                    data:  {
                        "fileContent": scope.data,
                        "fileName": scope.fileName
                    },*/
                    url:'/Ibm-Client/uploadUserFile.htm',
                    data:  formData,
                    async: false,
                    cache: false,
                    contentType: false,
                    processData: false,
                    beforeSend:function(){
                       // console.log("开始......");
                        goTime(0);
                    },
                    success:function(resp){

                        var resp = JSON.parse(resp);
                        if (resp.success) {
                           // $rootScope.$broadcast("getFileTables_EV");

                           // console.log("接受");

                            clearTimeout(timer);
                            $rootScope.key = Math.floor(Math.random()*10000000);
                            $rootScope.$broadcast("sheetFilter_EV",scope.fileName,$rootScope.key);
                            goTimeEnd(80);

                        }else{
                            clearTimeout(timer);
                            $('#upProgress').progress({
                                percent: 0
                            });
                           /* $('#upProgress2').progress({
                                percent: 0
                            });*/
                            $('#upProgress').hide();
//                            $('#upProgress2').hide();
                            $(element).find("#progress").hide();
//                            alert(resp.message);
                            scope.error=true;
                            $timeout(function(){
                                $rootScope.$broadcast("MESSAGE_EV","文件上传异常，请找管理员联系。");
                                return;
                            },500);
                        }
                    }
                });
            }
        },
        templateUrl: "/trend-new-BI/app/components/dbManager/upFileMsg/upFileMsgView.html"
    }
}]);
